package com.pq.service.impl;

import com.pq.dao.order.OrderCartDao;
import com.pq.pojo.order.OrderCart;
import com.pq.service.order.OrderCartService;

import com.pq.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderCartServiceImpl implements OrderCartService {

    @Autowired
    private OrderCartDao orderCartDao;

    @Override
    public List<OrderCart> findOrderCartByUserId(Integer userId, Integer flag, List<Long> orderCartIds) {
        if (userId == null || userId == 0)
            throw new IllegalArgumentException();
        List<OrderCart> lists = orderCartDao.findOrderCartByUserId(userId,flag,orderCartIds);
        if (lists == null || lists.size() == 0)
            throw new IllegalArgumentException();
        return lists;
    }

    @Override
    @Transactional
    public SysResult updateOrderCartCountsByCartId(Integer productCounts, Integer cartId, Integer userId) {
        if (cartId == null||productCounts == null || productCounts <1){ throw new IllegalArgumentException();}
        int rows = orderCartDao.updateOrderCartCountsByCartId(productCounts, cartId, userId);
        if (rows == 0) throw new IllegalArgumentException();
        return SysResult.success();
    }

    @Override
    public List<OrderCart> findOrderCartByProductId(Integer userId, Integer productId) {
        if (productId == null || productId == 0) throw new IllegalArgumentException();
        List<OrderCart> lists = orderCartDao.findOrderCartByProductId(userId, productId);
        if (lists == null || lists.size() == 0) throw new IllegalArgumentException();
        return lists;
    }

    @Override
    @Transactional
    public SysResult updateOrderCartFlagByCartId(Long orderCartId, Integer userId) {
        if (orderCartId == null || orderCartId < 1) throw new IllegalArgumentException();
        long rows = orderCartDao.updateOrderCartFlagByCartId(orderCartId, userId);
        if (rows == 0) throw new IllegalArgumentException();
        return SysResult.success();

    }

    @Override
    @Transactional
    public SysResult updateOrderCartFlagByCartIds(Integer userId, List<Long> orderCartIds) {
        if ( orderCartIds == null ) throw new IllegalStateException();
        int lists = orderCartDao.updateOrderCartFlagByCartIds(userId, orderCartIds);
        if ( lists == 0) throw new IllegalStateException();
        return SysResult.success();

    }

    @Override
    public int deleteByCartId(Long cartId) {
        int row = orderCartDao.deleteByCartId(cartId);
        return row;
    }
}
